package com.haois.component.util.excel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GCU
 * @date 2017/3/17 9:18
 */
public class UpExcelUtil {

    private Logger logger = LoggerFactory.getLogger(UpExcelUtil.class);


    public Map<String, Object> analyzeTitle(HSSFWorkbook workbook, String[] head) {
        Map<String, Object> map = new HashMap<String, Object>();

        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFRow row;
        HSSFCell cell;
        String sheetName = sheet.getSheetName();
        logger.info("读取的work表单名为{}", sheetName);
        map.put("orgId", sheetName);

        cell = sheet.getRow(0).getCell(0);
        String oName = cell.getStringCellValue();
        logger.info("读取的work头标题为{}", oName);
        map.put("orgInfo", oName);
        row = sheet.getRow(1);
        int rowSize = row.getLastCellNum();

        List<String> rows = new ArrayList<String>();
        for (int i = 0; i < rowSize; i = i + head.length) {
            cell = row.getCell(i);
            rows.add(cell.getStringCellValue());
        }
        map.put("deps", rows);
        return map;
    }
    public Map<String, Object> analyzePreTitle(HSSFWorkbook workbook, String[] head) {
        Map<String, Object> map = new HashMap<String, Object>();

        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFRow row;
        HSSFCell cell;
        String sheetName = sheet.getSheetName();
        logger.info("读取的work表单名为{}", sheetName);
        map.put("sheetName", sheetName);
        cell = sheet.getRow(0).getCell(0);
        String preName = cell.getStringCellValue();
        logger.info("读取的work头标题为{}", preName);
        map.put("preName", preName);

        row = sheet.getRow(2);
        List<String> rows = new ArrayList<String>();
        for (int i = 0; i < head.length; i++) {
            cell = row.getCell(i);
            rows.add(cell.getStringCellValue());
        }
        map.put("heads", rows);
        return map;
    }

    public HashMap<Integer, List<String[]>> analyzeContent(HSSFWorkbook workbook, String[] head, int groups) {
        return this.analyzeContent(workbook, head, 3, groups);
    }

    public HashMap<Integer, List<String[]>> analyzeContent(HSSFWorkbook workbook, String[] head, int ro, int groups) {

        HashMap<Integer, List<String[]>> hashMap = new HashMap<Integer, List<String[]>>();

        HSSFSheet sheet = workbook.getSheetAt(0);

        HSSFRow row;
        HSSFCell cell;

        int rowNum = sheet.getLastRowNum();
        int index = 0;
        int ro_ = ro;
        for (int co = 0; co < groups * head.length; co = co + head.length) {
            List<String[]> cons = new ArrayList<String[]>();

            for (ro = ro_; ro < rowNum; ro++) {
                String[] value = new String[head.length];
                for (int s = 0; s < head.length; s++) {
                    cell = sheet.getRow(ro).getCell(co + s);
                    if (cell != null) {
                        value[s] = cell.getStringCellValue();
                    } else {
                        value[s] = "";
                    }

                }
                if (value[index].isEmpty()) {
                    break;
                }
                cons.add(value);
            }
            hashMap.put(co / head.length, cons);
        }
        return hashMap;
    }

    public String getFileOid(HSSFWorkbook workbook) {


        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFCell cell;
        String sheetName = sheet.getSheetName();
        logger.info("读取的work表单名为{}", sheetName);

        cell = sheet.getRow(0).getCell(0);
        String oName = cell.getStringCellValue();
        return oName;

    }

}
